package com.basis.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;

@SuppressWarnings("unchecked")
public class GetReferenceListHandler extends BaseActionHandler {
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject result = new JSONObject();
		try {
			JSONObject request = new JSONObject(data);
			String id = request.getString("id");
			
			String sql = "select value, name from ad_ref_list where ad_reference_id = ? and isactive = 'Y'";
			List<Object[]> list = OBDal.getInstance().getSession().createSQLQuery(sql).setString(0, id).list();
			
			Map map = new HashMap();
			for (Object[] obj : list) {
				map.put(obj[0], obj[1]);
			}
			
			result.put("list", map);
			result.put("result", true);
			OBDal.getInstance().commitAndClose();
		} catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			logger.error("获取数据下拉异常：" + e.getMessage(), e);
			try {
				result.put("result", false);
				result.put("msg", "获取数据下拉异常");
			} catch (Exception e1) {
			}
		}
		return result;
	}
}
